package com.it.bookkeeping.greendao;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import com.lt.bookkeeping.DataBaseApplication;
import com.lt.bookkeeping.Utils.ThreadPoolUtils;
import com.lt.bookkeeping.dataSave.BookKeepData;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.ConsoleHandler;

/**
 * @author LiuTong
 */
public class Dao {

    private static Dao instance;
    private DaoSession daoSession;
    public static Dao getInstance(DaoSession daoSession){
        if(instance == null){
            synchronized (Dao.class){
                if(instance == null){
                    instance = new Dao(daoSession);
                }
            }
        }
        return instance;
    }
    private Dao(DaoSession daoSession){
        this.daoSession = daoSession;
    }
    public long insert(BookKeepData bookKeepData){
       return daoSession.insert(bookKeepData);
    }

    public void delete(BookKeepData bookKeepData){
        daoSession.delete(bookKeepData);
    }

    public  void deleteAll(){
        daoSession.deleteAll(BookKeepData.class);
    }

    public  void update(BookKeepData bookKeepData){
        daoSession.update(bookKeepData);
    }

    public List<BookKeepData> queryAll(){
        return daoSession.loadAll(BookKeepData.class);
    }
    public interface requestResult{
        /***
         * 呈贡数据返回
         * @param bookKeepDataList
         * @param counts 所有数据个数
         */
        void success(List<BookKeepData> bookKeepDataList,int counts);
        void fail();
    }


    private final Handler handler = new Handler(Looper.getMainLooper());

    public void query(int page,int countsInPage,requestResult requestResult){
        ThreadPoolUtils.getInstance().runTask(new Runnable() {
            @Override
            public void run() {
                List<BookKeepData> list =daoSession
                        .queryBuilder(BookKeepData.class)
                        .offset(page*countsInPage)
                        .limit(countsInPage)
                        .list();
                int size = daoSession.loadAll(BookKeepData.class).size();
                if(requestResult != null){
                    if(list != null && list.size()>0 && size > 0){
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                requestResult.success(list,size);
                            }
                        });
                    }else {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                requestResult.fail();
                            }
                        });
                    }

                }
            }
        });
    }

    public int size(){
        try {
            return daoSession.loadAll(BookKeepData.class).size();
        }catch (Exception e){
            return 0;
        }

    }

    public  List<BookKeepData> queryById(String id){
        return daoSession.queryRaw(BookKeepData.class,"where id = ?",id);
    }

    public  List<BookKeepData> queryByEvent( String event){
        QueryBuilder<BookKeepData> qb = daoSession.queryBuilder(BookKeepData.class);
        QueryBuilder<BookKeepData> qbEvent = qb.where(
                BookKeepDataDao.Properties.Event.like(event))
                .orderAsc(BookKeepDataDao.Properties.BookKeepTime);
        return qbEvent.list();
    }

    public  List<BookKeepData> queryByMoney(double money){
        QueryBuilder<BookKeepData> qb = daoSession.queryBuilder(BookKeepData.class);
        QueryBuilder<BookKeepData> qbMoney = qb.where(
                BookKeepDataDao.Properties.Money.eq(money))
                .orderAsc(BookKeepDataDao.Properties.BookKeepTime);
        return qbMoney.list();
    }


}
